package cn.e3.order.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.e3.cart.service.AddCartToRedis;
import cn.e3.order.pojo.TbOrderInfo;
import cn.e3.order.service.OrderService;
import cn.e3.pojo.TbItem;
import cn.e3.pojo.TbUser;
import cn.e3.utils.E3Result;

@Controller
public class OrderController {
	@Autowired
	private AddCartToRedis addCartToRedis;
	@Autowired
	private OrderService orderService;
	@RequestMapping("/order/order-cart")
	public String submitOrder(HttpServletRequest request){
		//从登录信息中得到用户信息
		TbUser user = (TbUser) request.getAttribute("user");
		//根据用户查询用户订单
		List<TbItem> cartList = addCartToRedis.showCart(user.getId());
		//查询到的订单转发到页面
		request.setAttribute("cartList", cartList);
		return "order-cart";
	}
	@RequestMapping(value="/order/create",method=RequestMethod.POST)
	public String createOrder(TbOrderInfo orderInfo,HttpServletRequest request){
		//获得当前登录的用户
		TbUser user = (TbUser) request.getAttribute("user");
		//补全pojo属性
		orderInfo.setUserId(user.getId());
		orderInfo.setBuyerNick(user.getUsername());
		//调用订单服务，执行数据库更新操作
		E3Result e3Result = orderService.createOrder(orderInfo);
		if(e3Result.getStatus()==200){
			//更新正常，情空当前用户购物车
			addCartToRedis.cleanCart(user.getId());
		}
		//将数据返回页面
		request.setAttribute("orderId", orderInfo.getOrderId());
		request.setAttribute("payment", orderInfo.getPayment());
		//返回逻辑视图
		return "success";
	}
}
